package com.zhang.sdk.ui.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.zhang.sdk.receiver.BluetoothStateBroadcastReceive;

/*****************************************************
 * 版权信息: 2019 北京振中电子有限公司版权所有
 * 创建作者: zhang.x
 * 创建日期: 2019/10/14 17:55
 * ****************************************************
 * 更改记录: 更新人:    更新时间:    更新概要:
 *
 * ****************************************************
 *  类功能说明: 包含监听蓝牙状态的活动基类
 * ****************************************************/
public class BluetoothActivity extends BaseActivity{
    private BluetoothStateBroadcastReceive mReceive;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBluetoothReceiver();
    }

    public void setBluetoothStateChangedListener(BluetoothStateBroadcastReceive.OnBluetoothStateChangedListener listener) {
        mReceive.setBluetoothStateChangedListener(listener);
    }

    @Override
    protected void onDestroy() {
        if(mReceive != null){
            unregisterReceiver(mReceive);
            mReceive = null;
        }
        super.onDestroy();
    }

    private void registerBluetoothReceiver(){
        if(mReceive == null){
            mReceive = new BluetoothStateBroadcastReceive();
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        intentFilter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        registerReceiver(mReceive, intentFilter);
    }
}
